package com.jieshuibao.service;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.jieshuibao.common.res.BaseRes;
import com.jieshuibao.mapper.QuestionExMapper;
import com.jieshuibao.mapper.QuestionMapper;
import com.jieshuibao.model.db.AnswerUser;
import com.jieshuibao.model.db.Praise;
import com.jieshuibao.model.db.Question;
import com.jieshuibao.model.db.QuestionEx;
import com.jieshuibao.model.db.QuestionExample;
import com.jieshuibao.model.db.QuestionUserAnswerRewardFlow;
import com.jieshuibao.model.db.QuestionUserEx;
import com.jieshuibao.model.db.QuestionUserKey;
import com.jieshuibao.model.domain.PagePhone;
import com.jieshuibao.model.domain.SerarchExample;
import com.jieshuibao.util.IdGen;
import com.jieshuibao.util.StringUtils;
import com.jieshuibao.wx.util.PayCommonUtil;

@Service
public class QuestionExService {

	@Resource
	private QuestionExMapper questionExMapper;
	@Resource
	private SensitivewordFilterService sensitivewordFilterService;
	@Resource
	private QuestionMapper questionMapper;
	
	@Resource
	private QuestionUserService questionUserService;
	
	@Resource
	private AnswerUserService answerUserService;
	
	@Resource
	private PraiseService praiseService;
	
	@Resource
	private CounselorDutyService counselorDutyService;
	
	@Value("${question.end.date}")
	private int questionEndDate;
	//我问的
	public List<QuestionEx> getPageQuestion(QuestionExample questionExample) {
		return questionExMapper.selectPageEx(questionExample);
	}
	//待揭榜
	public List<QuestionEx> getPageQuestionTwo(PagePhone pagePhone) {
		return questionExMapper.selectPageExTwo(pagePhone);
	}
	//偷偷看列表
	public List<QuestionUserEx> getPageQuesiotnUser(PagePhone pagePhone){
		return questionExMapper.selectPageQuestionUser(pagePhone);
	}
	public QuestionUserEx getQuestionExByUuid(String QUuid){
		return questionExMapper.selectQuestionUserByUuuid(QUuid);
	}
	 //根据问题id 查用户的phoneNum 
    public String getphoneNumByQuestionID(String questionUuid){
    	return questionExMapper.selectphoneNumByQuestionID(questionUuid);
    }
	//我回答的悬赏问题
	public List<QuestionUserAnswerRewardFlow> getQuestionUserAnswerRewardFlow(PagePhone pagePhone){
		return questionExMapper.selectMyAnswerQuestion(pagePhone);
	}
	//偷偷看分享列表
	public List<QuestionUserEx> getPageQuesiotnUserShare(PagePhone pagePhone){
		return questionExMapper.selectPageQuestionUserShare(pagePhone);
	}
	//搜索
	public List<QuestionUserEx> getQuestionByContent(PagePhone pagePhone){
		return questionExMapper.selectSearchQuestionUser(pagePhone);
	}
    //围观列表
	public List<QuestionEx> getOnlookQuestion(QuestionExample questionExample){
		List<QuestionEx> selectPageOnlook = questionExMapper.selectPageOnlook(questionExample);
		return selectPageOnlook;
	}
	//围观搜索
	
	public List<QuestionEx> getOnlookSercher(SerarchExample serarchExample){
		return questionExMapper.selectOnlookSerarch(serarchExample);
		
	}
	/**
	 * 完善发布的快速问
	 * @param question
	 * @return
	 */
	public Question perfectQuestion(Question question){
		if(StringUtils.isBlank(question.getUuid())){
			question.setUuid(IdGen.uuid());//主键
		}
		question.setStatus(1);//状态 1:未采纳答案；3：已采纳并公开；4：已采纳不公开；5：已结束
		question.setDate(new Date());
		Calendar calendar = Calendar.getInstance();
		//结束时间
		calendar.setTimeInMillis((question.getDate().getTime())
				+ ((long) questionEndDate) * 24 * 3600 * 1000);
		question.setEndDate(calendar.getTime());
		question.setPayStatus(PayCommonUtil.PAYPENDING_STATUS);//支付状态：1未支付  0 已支付
		return question;
	}
	/**
	 * 微信围观授权
	 * @param userId
	 * @param quuId
	 * @return
	 */
	public Map<String, Object> onlookAuthorized(String userId,String quuId){
		Map<String, Object> res = new HashMap<String, Object>();
		try {
			if (StringUtils.isBlank(quuId)||StringUtils.isBlank(userId)) {
				res.put("code", BaseRes.ERRORPARAM_CODE);
				res.put("des", BaseRes.ERRORPARAM_DES);
				return res;
			}
			
			QuestionUserEx question = questionExMapper.selectQuestionUserByUuuid(quuId);
			if (question==null) {
				res.put("code", BaseRes.ERRORPARAM_CODE);
				res.put("des", BaseRes.ERRORPARAM_DES);
				return res;
			}
			Integer status = question.getStatus();
			if (status!=null&&status==3) {
				String phoneNumber = question.getPhoneNumber();
				if (!userId.equals(phoneNumber)) {
					QuestionUserKey questionUserKey2 = new QuestionUserKey();
					questionUserKey2.setPhoneNumber(userId);
					questionUserKey2.setQuestionUuid(quuId);
					QuestionUserKey questionUserKey = questionUserService
							.getQuestionUserKeys(questionUserKey2);
					if (questionUserKey==null) {
						//需要购买
						res.put("questionUser", question);
						res.put("code", BaseRes.SUCCESS_CODE);
						res.put("des", BaseRes.SUCCESS_DES);
						res.put("isBuy", "1");//是否需要购买  1需要 0 不需要
						return res;
						
					}
				}
				
				List<AnswerUser> answerUsers = answerUserService
						.getAnswerUserByQUid(quuId);
				if (answerUsers!=null&&answerUsers.size()>0) {
					AnswerUser answerUser = answerUsers.get(0);
					Praise praise = praiseService.getPraise(userId, answerUser.getUuid());
					if (praise!=null) {
						Integer praiseNum = praise.getPraiseNum();
						Integer treadNum = praise.getTreadNum();
						if (praiseNum==null) {
							praiseNum=0;
						}
						if (treadNum==null) {
							treadNum=0;
						}
						answerUser.setPraiseNum(praiseNum);
						answerUser.setTreadNum(treadNum);
					}
					
					String counselorDuty = answerUser.getCounselorDuty();
					String dutyName = counselorDutyService.getCounselorDutyName(counselorDuty);
					answerUser.setCounselorDuty(dutyName);
					res.put("questionUser", question);
					res.put("answewrUser", answerUser);
					res.put("code", BaseRes.SUCCESS_CODE);
					res.put("des", BaseRes.SUCCESS_DES);
					res.put("isBuy", "0");//是否需要购买  1需要 0 不需要
					return res;
					
				}else {
					res.put("code", BaseRes.ERRORPARAM_CODE);
					res.put("des", BaseRes.ERRORPARAM_DES);
					return res;
				}
			}else {
				res.put("code", BaseRes.ERRORPARAM_CODE);
				res.put("des", BaseRes.ERRORPARAM_DES);
				return res;
			}
		} catch (Exception e) {
			e.printStackTrace();
			res.put("code", BaseRes.FAIL_CODE);
			res.put("des", BaseRes.FAIL_DES);
			return res;
		}
	}
	
	/**
	 * 抢答列表详情
	 * @param uuid
	 * @return
	 */
	public QuestionUserEx grabQuestionDetais(String uuid,String userId){
		
		
		
		
		return questionExMapper.selectQuestionUserByUuuid(uuid);
	}
	
}
